﻿using Appliaction.Dto.Inv;
using Appliaction.IAppServices.Inv;
using AutoMapper;
using Domain.Entitys.Inv;
using Domain.IRepositorys.Inv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appliaction.AppServices.Inv
{
    public class Inv_QtyServices : IInv_QtyServices
    {
        private readonly IInv_QtyRepository  inv_QtyRepository;

        private readonly IMapper mapper;

        public Inv_QtyServices(IInv_QtyRepository inv_QtyRepository, IMapper mapper)
        {
            this.inv_QtyRepository = inv_QtyRepository;
            this.mapper = mapper;
        }
      


        /// <summary>
        /// 添加明细表--盘点
        /// </summary>
        /// <param name="inv_proddtiList"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> inv_proddti_Insert(List<Inv_Proddti_AddDto> inv_proddtiList)
        {
            var emtite= inv_proddtiList.Select(dto=>new inv_proddti
            {
                Qty_id = dto.Qty_id,
                Pro_name = dto.Pro_name,
                Pro_code = dto.Pro_code,
                Pro_brand = dto.Pro_brand,
                Unitmeas_id = dto.Unitmeas_id,
                Pro_model = dto.Pro_model,
                Pro_cost_price = dto.Pro_cost_price,
                Pro_sale_price = dto.Pro_sale_price,
                Location_code = dto.Location_code,
                Location_name = dto.Location_name,
                Inventory_numder = dto.Inventory_numder,
                Proddti_loss = dto.Proddti_loss,
                Proddti_profit = dto.Proddti_profit,
                Proddti_profit_money = dto.Proddti_profit_money,
                Proddti_loss_money = dto.Proddti_loss_money,
                stock_entry_no= dto.stock_entry_no
            }).ToList();
            return await inv_QtyRepository.inv_proddti_Insert(emtite);
        }
        

        /// <summary>
        /// 删除盘点单
        /// </summary>
        /// <param name="qty_id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_Qty_delete(int qty_id)
        {
            return inv_QtyRepository.Inv_Qty_delete(qty_id);
        }

        /// <summary>
        /// 修改审核状态--盘点
        /// </summary>
        /// <param name="inv_QtyUpdateDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_qty_examineUpdate(Inv_QtyUpdateDto inv_QtyUpdateDto)
        {
           Inv_qty inv_qty = mapper.Map<Inv_qty>(inv_QtyUpdateDto);
            return inv_QtyRepository.Inv_Qty_examineUpdate(inv_qty);
        }
        /// <summary>
        /// 根据盘点单号查询盘点单id
        /// </summary>
        /// <param name="Qty_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> inv_Qty_id(string Qty_code)
        {
           return inv_QtyRepository.inv_Qty_id(Qty_code);
        }


        /// <summary>
        /// 根据编号查询盘点信息
        /// </summary>
        /// <param name="qty_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<List<Inv_qty>> Inv_Qty_SelectCode(string qty_code)
        {
           return inv_QtyRepository.Inv_Qty_SelectCode(qty_code);
        }
        /// <summary>
        /// 查询总数
        /// </summary>
        /// <param name="qty_code"></param>
        /// <param name="qty_type"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_Qty_selectCount(string qty_code, string qty_type, string proName)
        {
           return inv_QtyRepository.Inv_Qty_selectCount(qty_code, qty_type,proName);
        }
        /// <summary>
        /// 根据库存盘点查询明细
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<List<inv_proddti>> Inv_Qty_SelectDetail(string qty_code)
        {
          return inv_QtyRepository.Inv_Qty_SelectDetail(qty_code);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="qty_code"></param>
        /// <param name="qty_type"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Qty_SelectDto> Inv_Qty_selectFY(string qty_code, string qty_type, int currentPage, int pageSize, string proName)
        {
            return inv_QtyRepository.Inv_Qty_selectFY(qty_code, qty_type, currentPage, pageSize,proName);
        }
        /// <summary>
        ///  修改盘点单和详情单
        /// </summary>
        /// <param name="inv_QtyUpdateMerageDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_Qty_Update(Inv_QtyUpdateMerageDto inv_QtyUpdateMerageDto)
        {
           Inv_qty inv_Qty = mapper.Map<Inv_qty>(inv_QtyUpdateMerageDto.inv_QtyUpdateDetailsDto);
            return inv_QtyRepository.Inv_Qty_Update(inv_Qty, inv_QtyUpdateMerageDto.inv_proddtiList);
        }

        /// <summary>
        /// 添加盘点单
        /// </summary>
        /// <param name="inv_QtyAddDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_trf_Insert(Inv_QtyAddDto inv_QtyAddDto)
        {
           Inv_qty inv_qty = mapper.Map<Inv_qty>(inv_QtyAddDto);
            return inv_QtyRepository.Inv_trf_Insert(inv_qty);
        }



        /// <summary>
        /// 其他入库单查询盘点单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<Inv_qty> Inv_Qty_MainSelect(string code, int currentPage, int pageSize) {

            return inv_QtyRepository.Inv_Qty_MainSelect(code, currentPage, pageSize);
        }
        /// <summary>
        /// 其他入库单查询盘点单--其他入库单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Task<int> Inv_Qty_MainSelect_Count(string code) {

            return inv_QtyRepository.Inv_Qty_MainSelect_Count(code);
        }

        /// <summary>
        /// 根据编码查询仓库盘点详情信息--其他入库单
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        public Task<List<inv_proddti>> Inv_Qty_SelectMainDetail(string qty_code) {

            return inv_QtyRepository.Inv_Qty_SelectMainDetail(qty_code);
        }

        /// <summary>
        /// 盘亏出库--其他出库单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<Inv_qty> Inv_Qty_Maisbhg_Select(string code, int currentPage, int pageSize) { 
        
        return inv_QtyRepository.Inv_Qty_Maisbhg_Select(code, currentPage, pageSize);
        }

        /// <summary>
        /// 其他入库单查询盘点单--其他出库单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Task<int> Inv_Qty_Maisbhg_Select_Count(string code) {

            return inv_QtyRepository.Inv_Qty_Maisbhg_Select_Count(code);
        }

        /// <summary>
        /// 根据编码查询仓库盘点详情信息--其他出库单
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        public Task<List<inv_proddti>> Inv_Qty_SelectMainbhgDetail(string qty_code) { 
        
        return inv_QtyRepository.Inv_Qty_SelectMainbhgDetail(qty_code);
        }


        /// <summary>
        /// 盘点详情修改状态--其他入库单完毕之后
        /// </summary>
        /// <param name="inv__proddtiUpdateztDtos"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_Qty_Update_zt(List<Inv__proddtiUpdateztDto> inv__proddtiUpdateztDtos)
        {
            var entis = inv__proddtiUpdateztDtos.Select(dto => new inv_proddti
            {
                 Pro_code=dto.Pro_code,
               
                Location_code = dto.location_code,
                stock_entry_no = dto.stock_entry_no
            }).ToList();
            return inv_QtyRepository.Inv_Qty_Update_zt(entis);
        }

        /// <summary>
        /// 其他出库单审核完毕之后,修改盘点单的qty_store_bhg这个字段为1
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public Task<int> Inv_qty_update_qty_store_bhg(string code) { 
        
        return inv_QtyRepository.Inv_qty_update_qty_store_bhg(code);
        }
    }
}
